import {shallowRef,ref} from 'vue'
import axios from 'axios'
import {provideGlobalConfig} from 'element-plus'
import {getApp} from 'framework'
const elementPlusLang={
    zh:{
        name: "zh-cn",
        el: {
            breadcrumb: {
                label: "\u9762\u5305\u5C51"
            },
            colorpicker: {
                confirm: "\u786E\u5B9A",
                clear: "\u6E05\u7A7A",
                defaultLabel: "\u989C\u8272\u9009\u62E9\u5668",
                description: "\u5F53\u524D\u989C\u8272 {color}\uFF0C\u6309 Enter \u952E\u9009\u62E9\u65B0\u989C\u8272",
                alphaLabel: "\u9009\u62E9\u900F\u660E\u5EA6\u7684\u503C"
            },
            datepicker: {
                now: "\u6B64\u523B",
                today: "\u4ECA\u5929",
                cancel: "\u53D6\u6D88",
                clear: "\u6E05\u7A7A",
                confirm: "\u786E\u5B9A",
                dateTablePrompt: "\u4F7F\u7528\u65B9\u5411\u952E\u4E0E Enter \u952E\u53EF\u9009\u62E9\u65E5\u671F",
                monthTablePrompt: "\u4F7F\u7528\u65B9\u5411\u952E\u4E0E Enter \u952E\u53EF\u9009\u62E9\u6708\u4EFD",
                yearTablePrompt: "\u4F7F\u7528\u65B9\u5411\u952E\u4E0E Enter \u952E\u53EF\u9009\u62E9\u5E74\u4EFD",
                selectedDate: "\u5DF2\u9009\u65E5\u671F",
                selectDate: "\u9009\u62E9\u65E5\u671F",
                selectTime: "\u9009\u62E9\u65F6\u95F4",
                startDate: "\u5F00\u59CB\u65E5\u671F",
                startTime: "\u5F00\u59CB\u65F6\u95F4",
                endDate: "\u7ED3\u675F\u65E5\u671F",
                endTime: "\u7ED3\u675F\u65F6\u95F4",
                prevYear: "\u524D\u4E00\u5E74",
                nextYear: "\u540E\u4E00\u5E74",
                prevMonth: "\u4E0A\u4E2A\u6708",
                nextMonth: "\u4E0B\u4E2A\u6708",
                year: "\u5E74",
                month1: "1 \u6708",
                month2: "2 \u6708",
                month3: "3 \u6708",
                month4: "4 \u6708",
                month5: "5 \u6708",
                month6: "6 \u6708",
                month7: "7 \u6708",
                month8: "8 \u6708",
                month9: "9 \u6708",
                month10: "10 \u6708",
                month11: "11 \u6708",
                month12: "12 \u6708",
                weeks: {
                    sun: "\u65E5",
                    mon: "\u4E00",
                    tue: "\u4E8C",
                    wed: "\u4E09",
                    thu: "\u56DB",
                    fri: "\u4E94",
                    sat: "\u516D"
                },
                weeksFull: {
                    sun: "\u661F\u671F\u65E5",
                    mon: "\u661F\u671F\u4E00",
                    tue: "\u661F\u671F\u4E8C",
                    wed: "\u661F\u671F\u4E09",
                    thu: "\u661F\u671F\u56DB",
                    fri: "\u661F\u671F\u4E94",
                    sat: "\u661F\u671F\u516D"
                },
                months: {
                    jan: "\u4E00\u6708",
                    feb: "\u4E8C\u6708",
                    mar: "\u4E09\u6708",
                    apr: "\u56DB\u6708",
                    may: "\u4E94\u6708",
                    jun: "\u516D\u6708",
                    jul: "\u4E03\u6708",
                    aug: "\u516B\u6708",
                    sep: "\u4E5D\u6708",
                    oct: "\u5341\u6708",
                    nov: "\u5341\u4E00\u6708",
                    dec: "\u5341\u4E8C\u6708"
                }
            },
            inputNumber: {
                decrease: "\u51CF\u5C11\u6570\u503C",
                increase: "\u589E\u52A0\u6570\u503C"
            },
            select: {
                loading: "\u52A0\u8F7D\u4E2D",
                noMatch: "\u65E0\u5339\u914D\u6570\u636E",
                noData: "\u65E0\u6570\u636E",
                placeholder: "\u8BF7\u9009\u62E9"
            },
            dropdown: {
                toggleDropdown: "\u5207\u6362\u4E0B\u62C9\u9009\u9879"
            },
            mention: {
                loading: "\u52A0\u8F7D\u4E2D"
            },
            cascader: {
                noMatch: "\u65E0\u5339\u914D\u6570\u636E",
                loading: "\u52A0\u8F7D\u4E2D",
                placeholder: "\u8BF7\u9009\u62E9",
                noData: "\u6682\u65E0\u6570\u636E"
            },
            pagination: {
                goto: "\u524D\u5F80",
                pagesize: "\u6761/\u9875",
                total: "\u5171 {total} \u6761",
                pageClassifier: "\u9875",
                page: "\u9875",
                prev: "\u4E0A\u4E00\u9875",
                next: "\u4E0B\u4E00\u9875",
                currentPage: "\u7B2C {pager} \u9875",
                prevPages: "\u5411\u524D {pager} \u9875",
                nextPages: "\u5411\u540E {pager} \u9875",
                deprecationWarning: "\u4F60\u4F7F\u7528\u4E86\u4E00\u4E9B\u5DF2\u88AB\u5E9F\u5F03\u7684\u7528\u6CD5\uFF0C\u8BF7\u53C2\u8003 el-pagination \u7684\u5B98\u65B9\u6587\u6863"
            },
            dialog: {
                close: "\u5173\u95ED\u6B64\u5BF9\u8BDD\u6846"
            },
            drawer: {
                close: "\u5173\u95ED\u6B64\u5BF9\u8BDD\u6846"
            },
            messagebox: {
                title: "\u63D0\u793A",
                confirm: "\u786E\u5B9A",
                cancel: "\u53D6\u6D88",
                error: "\u8F93\u5165\u7684\u6570\u636E\u4E0D\u5408\u6CD5!",
                close: "\u5173\u95ED\u6B64\u5BF9\u8BDD\u6846"
            },
            upload: {
                deleteTip: "\u6309 delete \u952E\u53EF\u5220\u9664",
                delete: "\u5220\u9664",
                preview: "\u67E5\u770B\u56FE\u7247",
                continue: "\u7EE7\u7EED\u4E0A\u4F20"
            },
            slider: {
                defaultLabel: "\u6ED1\u5757\u4ECB\u4E8E {min} \u81F3 {max}",
                defaultRangeStartLabel: "\u9009\u62E9\u8D77\u59CB\u503C",
                defaultRangeEndLabel: "\u9009\u62E9\u7ED3\u675F\u503C"
            },
            table: {
                emptyText: "\u6682\u65E0\u6570\u636E",
                confirmFilter: "\u7B5B\u9009",
                resetFilter: "\u91CD\u7F6E",
                clearFilter: "\u5168\u90E8",
                sumText: "\u5408\u8BA1"
            },
            tour: {
                next: "\u4E0B\u4E00\u6B65",
                previous: "\u4E0A\u4E00\u6B65",
                finish: "\u7ED3\u675F\u5BFC\u89C8"
            },
            tree: {
                emptyText: "\u6682\u65E0\u6570\u636E"
            },
            transfer: {
                noMatch: "\u65E0\u5339\u914D\u6570\u636E",
                noData: "\u65E0\u6570\u636E",
                titles: ["\u5217\u8868 1", "\u5217\u8868 2"],
                filterPlaceholder: "\u8BF7\u8F93\u5165\u641C\u7D22\u5185\u5BB9",
                noCheckedFormat: "\u5171 {total} \u9879",
                hasCheckedFormat: "\u5DF2\u9009 {checked}/{total} \u9879"
            },
            image: {
                error: "\u52A0\u8F7D\u5931\u8D25"
            },
            pageHeader: {
                title: "\u8FD4\u56DE"
            },
            popconfirm: {
                confirmButtonText: "\u786E\u5B9A",
                cancelButtonText: "\u53D6\u6D88"
            },
            carousel: {
                leftArrow: "\u4E0A\u4E00\u5F20\u5E7B\u706F\u7247",
                rightArrow: "\u4E0B\u4E00\u5F20\u5E7B\u706F\u7247",
                indicator: "\u5E7B\u706F\u7247\u5207\u6362\u81F3\u7D22\u5F15 {index}"
            }
        }
    },
    en:{
        name: "en",
        el: {
            breadcrumb: {
                label: "Breadcrumb"
            },
            colorpicker: {
                confirm: "OK",
                clear: "Clear",
                defaultLabel: "color picker",
                description: "current color is {color}. press enter to select a new color.",
                alphaLabel: "pick alpha value"
            },
            datepicker: {
                now: "Now",
                today: "Today",
                cancel: "Cancel",
                clear: "Clear",
                confirm: "OK",
                dateTablePrompt: "Use the arrow keys and enter to select the day of the month",
                monthTablePrompt: "Use the arrow keys and enter to select the month",
                yearTablePrompt: "Use the arrow keys and enter to select the year",
                selectedDate: "Selected date",
                selectDate: "Select date",
                selectTime: "Select time",
                startDate: "Start Date",
                startTime: "Start Time",
                endDate: "End Date",
                endTime: "End Time",
                prevYear: "Previous Year",
                nextYear: "Next Year",
                prevMonth: "Previous Month",
                nextMonth: "Next Month",
                year: "",
                month1: "January",
                month2: "February",
                month3: "March",
                month4: "April",
                month5: "May",
                month6: "June",
                month7: "July",
                month8: "August",
                month9: "September",
                month10: "October",
                month11: "November",
                month12: "December",
                week: "week",
                weeks: {
                    sun: "Sun",
                    mon: "Mon",
                    tue: "Tue",
                    wed: "Wed",
                    thu: "Thu",
                    fri: "Fri",
                    sat: "Sat"
                },
                weeksFull: {
                    sun: "Sunday",
                    mon: "Monday",
                    tue: "Tuesday",
                    wed: "Wednesday",
                    thu: "Thursday",
                    fri: "Friday",
                    sat: "Saturday"
                },
                months: {
                    jan: "Jan",
                    feb: "Feb",
                    mar: "Mar",
                    apr: "Apr",
                    may: "May",
                    jun: "Jun",
                    jul: "Jul",
                    aug: "Aug",
                    sep: "Sep",
                    oct: "Oct",
                    nov: "Nov",
                    dec: "Dec"
                }
            },
            inputNumber: {
                decrease: "decrease number",
                increase: "increase number"
            },
            select: {
                loading: "Loading",
                noMatch: "No matching data",
                noData: "No data",
                placeholder: "Select"
            },
            mention: {
                loading: "Loading"
            },
            dropdown: {
                toggleDropdown: "Toggle Dropdown"
            },
            cascader: {
                noMatch: "No matching data",
                loading: "Loading",
                placeholder: "Select",
                noData: "No data"
            },
            pagination: {
                goto: "Go to",
                pagesize: "/page",
                total: "Total {total}",
                pageClassifier: "",
                page: "Page",
                prev: "Go to previous page",
                next: "Go to next page",
                currentPage: "page {pager}",
                prevPages: "Previous {pager} pages",
                nextPages: "Next {pager} pages",
                deprecationWarning: "Deprecated usages detected, please refer to the el-pagination documentation for more details"
            },
            dialog: {
                close: "Close this dialog"
            },
            drawer: {
                close: "Close this dialog"
            },
            messagebox: {
                title: "Message",
                confirm: "OK",
                cancel: "Cancel",
                error: "Illegal input",
                close: "Close this dialog"
            },
            upload: {
                deleteTip: "press delete to remove",
                delete: "Delete",
                preview: "Preview",
                continue: "Continue"
            },
            slider: {
                defaultLabel: "slider between {min} and {max}",
                defaultRangeStartLabel: "pick start value",
                defaultRangeEndLabel: "pick end value"
            },
            table: {
                emptyText: "No Data",
                confirmFilter: "Confirm",
                resetFilter: "Reset",
                clearFilter: "All",
                sumText: "Sum"
            },
            tour: {
                next: "Next",
                previous: "Previous",
                finish: "Finish"
            },
            tree: {
                emptyText: "No Data"
            },
            transfer: {
                noMatch: "No matching data",
                noData: "No data",
                titles: ["List 1", "List 2"],
                filterPlaceholder: "Enter keyword",
                noCheckedFormat: "{total} items",
                hasCheckedFormat: "{checked}/{total} checked"
            },
            image: {
                error: "FAILED"
            },
            pageHeader: {
                title: "Back"
            },
            popconfirm: {
                confirmButtonText: "Yes",
                cancelButtonText: "No"
            },
            carousel: {
                leftArrow: "Carousel arrow left",
                rightArrow: "Carousel arrow right",
                indicator: "Carousel switch to index {index}"
            }
        }
    }
}

const setElementPlusLang=(_lang)=>{
    const locale=elementPlusLang[_lang];
    if (locale){
        provideGlobalConfig({locale},getApp())
    }
}

const baseMessages={
    zh:{
        "Add":"新增",
        "Save":"保存",
        "Modify":"修改",
        "Edit":"编辑",
        "Delete":"删除",
        "Search":"搜索",
        "Reset":"重置",
        "Cancel":"取消",
        "Confirm":"确认",
        "Enable":"启用",
        "Disable":"禁用",
        "Export":"导出",
        "Refresh":"刷新",
        "Detail":"详情",
        "Icon":"图标",
        "Email":"电子邮箱",
        "Saved":'已保存',
        "Close":"关闭",
        "SelectAll":'全选',
        "Are you sure to delete the selected record?":"确定删除所选记录？"
    }
}
baseMessages.en = Object.fromEntries(
    Object.entries(baseMessages.zh).map(([key, value]) => [value, key])
);
//const messages=shallowRef({})
const lang=ref('')
if (localStorage.lang){
    lang.value = localStorage.lang
}else{
   if (navigator.language.startsWith("zh")){
      // setLanguage("zh")
       lang.value='zh'
   }else{
       lang.value='en'
   }
}
addEventListener('appCreated',e=>{
    setElementPlusLang(lang.value)
})
export const i18n={
    locale:getLanguage(),
    setLocaleMessage(local,newLocalMessage){
        //console.log(local, newLocalMessage)
    },getLocaleMessage(en){
        return {}
    },hlang(path,options){
        return null
    }
}
export function decode(langStr){
    if(!langStr){
        return langStr;
    }
    if(langStr.startsWith("{")&&langStr.endsWith("}")){
        let langMap=JSON.parse(langStr);
        let keys= Object.keys(langMap);
        if(keys.length==0){
            return "";
        }
        let real=langMap[lang.value]
        if(real!=null){
            return real;
        }
        return langMap[keys[0]];
    }
    return langStr;
}
export function $t(zh,en){
   if (!en){
       if (lang.value=='zh'){
           return zh
       }else{
           let base = baseMessages.en[zh]
           if (base){
               return base
           }else{
               return zh
           }
       }
   }else{
       return lang.value=='en'?en:zh
   }
}
export function setLanguage(_lang){
    lang.value = _lang
    localStorage.lang=_lang
    i18n.locale=_lang
    setElementPlusLang(_lang)
}
export function getLanguage(){
    return lang.value
}
export function useI18n(messages={}){

   // const base=Object.assign({},baseMessages)
    messages=Object.assign({},messages)
    if (!messages.zh){
        messages.zh=baseMessages.zh
    }else{
        messages.zh=Object.assign({},messages.zh,baseMessages.zh)
    }

    return {
        t(msg,...args){
            let message=messages[lang.value]
            if (!message){
                return msg
            }
            let retMsg=message[msg]
            if (!retMsg){
                return msg
            }
            if (args){
                for(let i=0;i<args.length;i++){
                    retMsg=retMsg.replace(new RegExp("\\{0\\}",'g'),args[i])
                }
            }
            return retMsg
        }
    }
}
